package com.uzai.console.service.md.mdpageset.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.Const;
import com.uzai.console.dto.md.mdactivity.MdActivityQuery;
import com.uzai.console.dto.md.mdpageset.MdPageSetDto;
import com.uzai.console.dto.md.mdpageset.MdPageSetUserWelfareDto;
import com.uzai.console.entity.MdActivity;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MdActivityMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.md.mdpageset.MdPageSetService;
import com.uzai.console.vo.base.SelectKeyForMdPageSetVo;
import com.uzai.console.vo.md.mdactivity.MdActivityVo;
import com.uzai.console.vo.md.mdpageset.MdPageSetVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class MdPageSetServiceImpl implements MdPageSetService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MdActivityMapper mdActivityMapper;

    /**
     * 查询免单页面设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public MdPageSetVo findMdPageSet(Long merId){

        MdPageSetVo mdPageSetVo = new MdPageSetVo();

        //查询运营商免单页面设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.MD_PAGE_CONFIG.getValue());
        if(merBaseConfig != null ){
            try {
                mdPageSetVo =  JSONObject.parseObject(merBaseConfig.getVal(), MdPageSetVo.class);
            }catch (Exception e){

            }

        }
        return mdPageSetVo;
    }

    /**
     * 修改免单页面设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param mdPageSetDto
     * @return null
     */
    public void updateMdPageSet(MdPageSetDto mdPageSetDto){

        MdPageSetVo mdPageSetVo = new MdPageSetVo();
        BeanUtils.copyProperties(mdPageSetDto, mdPageSetVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(mdPageSetDto.getMerId(), MerConfigKeyEnum.MD_PAGE_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(mdPageSetVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(mdPageSetDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.MD_PAGE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(mdPageSetVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }
    }

    /**
     * 免单页面设置查询新会员福利跳转下拉框列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param mdPageSetUserWelfareDto
     * @return null
     */
    public List<SelectKeyForMdPageSetVo> findNewUserWelfareUrlListSelect(MdPageSetUserWelfareDto mdPageSetUserWelfareDto){

        //返回下拉列表
        List<SelectKeyForMdPageSetVo> selectIdVoList = new ArrayList<>();

        //查询活动列表
        MdActivityQuery mdActivityQuery = new MdActivityQuery();
        mdActivityQuery.setMerId(mdPageSetUserWelfareDto.getMerId());
        mdActivityQuery.setActivityType(mdPageSetUserWelfareDto.getActivityType());
        List<MdActivity> mdActivityList = mdActivityMapper.findByList(mdActivityQuery);
        if(mdActivityList != null && mdActivityList.size() > 0){
            for (MdActivity mdActivity : mdActivityList) {
                MdActivityVo mdActivityVo = new MdActivityVo();
                BeanUtils.copyProperties(mdActivity, mdActivityVo);
                SelectKeyForMdPageSetVo selectKeyVo_mdactivity = new SelectKeyForMdPageSetVo();
                selectKeyVo_mdactivity.setKey(mdActivityVo.getActivityUrl());
                selectKeyVo_mdactivity.setValue(mdActivityVo.getName());
                selectKeyVo_mdactivity.setType(0);
                selectIdVoList.add(selectKeyVo_mdactivity);
            }
        }

        //封装CMS首页
        SelectKeyForMdPageSetVo selectKeyVo_cms_homePage = new SelectKeyForMdPageSetVo();
        selectKeyVo_cms_homePage.setKey(Const.SystemUrl.CMS_HOME_URL);
        selectKeyVo_cms_homePage.setValue("CMS首页");
        selectKeyVo_cms_homePage.setType(1);
        selectIdVoList.add(selectKeyVo_cms_homePage);

        //封装CMS个人中心
        SelectKeyForMdPageSetVo selectKeyVo_cms_center = new SelectKeyForMdPageSetVo();
        selectKeyVo_cms_center.setKey(Const.SystemUrl.CMS_PERSONAL_CENTER_URL);
        selectKeyVo_cms_center.setValue("CMS个人中心");
        selectKeyVo_cms_center.setType(2);
        selectIdVoList.add(selectKeyVo_cms_center);

        return selectIdVoList;
    }

}
